package com.chb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.mapper.RoleMapper;
import com.chb.system.service.RoleService;
import com.chb.system.service.UserService;
import com.chb.system.vo.RoleVo;
import com.hos.entity.sys.CheckItem;
import com.hos.entity.sys.Role;
import com.hos.entity.sys.User;
import com.hos.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ：张家琪
 * @date ：Created in 2023/12/12 20:12
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserService userService;

    @Override
    public Page<Role> findRoleByCond(RoleVo roleVo) {
        Page<Role> page = new Page<>(roleVo.getPageNum(), roleVo.getPageSize());
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(roleVo.getRoleName())) {
            wrapper.like(Role::getRoleName, roleVo.getRoleName());
        }
        if (StringUtils.hasText(roleVo.getRoleCode())) {
            wrapper.like(Role::getRoleCode, roleVo.getRoleCode());
        }
        if (StringUtils.hasText(roleVo.getStatus())) {
            wrapper.eq(Role::getStatus, roleVo.getStatus());
        }
        if (Objects.nonNull(roleVo.getDateRange())) {
            wrapper.between(Role::getCreateTime, roleVo.getDateRange()[0], roleVo.getDateRange()[1]);
        }
        wrapper.orderByAsc(Role::getRoleSort);
        return roleMapper.selectPage(page, wrapper);
    }

    /**
     * 查询所有的可用角色
     *
     * @return
     */
    @Override
    public List<Role> selectAllRole() {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getStatus, 0);
        return roleMapper.selectList(wrapper);
    }

    // 添加
    @Override
    public Integer addRole(Role role) {
        // 创建时间
        role.setCreateTime(new Date());
        role.setCreateBy(getUserByToken().getCreateBy());
        return roleMapper.insert(role);
    }

    // 删除、批量删除
    @Override
    public Integer deleteRoleByIds(List<Long> ids) {
        return roleMapper.deleteBatchIds(ids);
    }

    // 根据id查询一个角色信息
    @Override
    public Role getRoleById(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    // 修改
    @Override
    public Integer updateRole(Role role) {
        role.setUpdateBy(getUserByToken().getUpdateBy());
        return roleMapper.updateById(role);
    }

    @Override
    public List<Role> findRolesByUserId(Long userId) {

        return roleMapper.selectRoleByUserId(userId);
    }

    /**
     * @return com.hos.entity.sys.User
     * @create by: 陈海彬
     * @description: 获取请求用户的信息
     * @create time: 2023/12/12 10:39
     */
    private User getUserByToken() {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        return userService.findUserByPhone(phone);
    }
}